package com.bracketbird.clientcore.util;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.*;
import com.google.gwt.user.client.*;
import com.bracketbird.clientcore.gui.*;

import java.util.*;

/**
 *
 */
public class GlobalKeyboardHandler {

    private ArrayList<Element> keyboardEventReceivers = new ArrayList<Element>();

    /**
     * Widgets can register their DOM element object if they would like to be a
     * trigger to intercept keyboard events
     */
    public void registerForKeyboardEvents(Element e) {
        this.keyboardEventReceivers.add(e);
    }

    /**
     * Returns true if this is one of the keys we are interested in
     */
    public boolean isInterestingKeycode(int keycode) {
        // ...
        return false;
    }


    public void setupSingleAppKeyboardShortcuts() {
        // Define an inner class to handle the event
        Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
            public void onPreviewNativeEvent(Event.NativePreviewEvent preview) {
                NativeEvent event = preview.getNativeEvent();

                Element elt = event.getEventTarget().cast();
                int keycode = event.getKeyCode();
                boolean ctrl = event.getCtrlKey();
                boolean shift = event.getShiftKey();
                boolean alt = event.getAltKey();
                boolean meta = event.getMetaKey();
                String type = event.getType();

                
                if(keycode == 27){//ESC
                     PopupManager.hide();
                }

                if (ctrl && keycode==77 && type.equalsIgnoreCase("keydown")){//ctrl + m
                    //PopUpDataPageController.showCreateMember();
                }
                handleKeycode(keycode);

                // Tell the event handler that this event has been consumed
                preview.consume();
            }
        });
    }



    /**
     * Setup the event preview class when the module is loaded.
     */
    public void setupKeyboardShortcuts() {
        // Define an inner class to handle the event
        Event.addNativePreviewHandler(new Event.NativePreviewHandler() {
            public void onPreviewNativeEvent(Event.NativePreviewEvent preview) {

                NativeEvent event = preview.getNativeEvent();



                Element elt = event.getEventTarget().cast();
                

                int keycode = event.getKeyCode();
                boolean ctrl = event.getCtrlKey();
                boolean shift = event.getShiftKey();
                boolean alt = event.getAltKey();
                boolean meta = event.getMetaKey();
                String type = event.getType();

                if(keycode == 27){//ESC
                     PopupManager.hide();
                }

                if (ctrl && keycode==77 && type.equalsIgnoreCase("keydown")){//ctrl + m
                    //PopUpDataPageController.showCreateMember();

                }
                handleKeycode(keycode);

                // Tell the event handler that this event has been consumed
                preview.cancel();
            }
        });
    }






    /**
     * Perform the keycode specific processing
     */
    private void handleKeycode(int keycode) {
        switch (keycode) {
            // ...
        }
    }

}
